package com.wcl.nullmusic.ui.activity;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.wcl.nullmusic.R;
import com.wcl.nullmusic.base.BaseActivity;
import com.wcl.nullmusic.utils.ToActivityUtils;
import com.wcl.nullmusic.view.CustomGridLayout;

import cn.jpush.android.api.JPushInterface;


/**
 * Created by dllo on 15/11/17.
 */

public class SplashActivity extends BaseActivity {
    private CustomGridLayout custom_gridLayout;
    /**
     * 透明动画
     **/
    Animation mAnimation = null;
    View view;
    ImageView alpha_image;
    private Runnable r = new Runnable() {
        @Override
        public void run() {
            int randomNumber = (int) (Math.random() * 15);
            //  image_item.setImageResource(srcs[i-1]);
            //  image_item.startAnimation(mAnimation);
            view = custom_gridLayout.getChildAt(randomNumber);
            alpha_image = (ImageView) view.findViewById(R.id.alpha_image);
            alpha_image.startAnimation(mAnimation);
            // view.clearAnimation();
            handler.postDelayed(r, 1500);
        }
    };
    private Handler handler;


    private int[] srcs = {R.mipmap.ic_welcome_photo_1, R.mipmap.ic_welcome_photo_2,
            R.mipmap.ic_welcome_photo_3, R.mipmap.ic_welcome_photo_4,
            R.mipmap.ic_welcome_photo_5, R.mipmap.ic_welcome_photo_6,
            R.mipmap.ic_welcome_photo_7, R.mipmap.ic_welcome_photo_8,
            R.mipmap.ic_welcome_photo_9, R.mipmap.ic_welcome_photo_10,
            R.mipmap.ic_welcome_photo_11, R.mipmap.ic_welcome_photo_12,
            R.mipmap.ic_welcome_photo_13, R.mipmap.ic_welcome_photo_14,
            R.mipmap.ic_welcome_photo_15};

    @Override
    protected void conService() {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void getVaule() {

    }

    @Override
    protected void initView() {
        JPushInterface.init(this);
        handler = new Handler();
        mAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
        custom_gridLayout = findView(R.id.custom_gridLayout);
    }

    @Override
    protected void initData() {
        custom_gridLayout.setGridAdapter(new CustomGridLayout.GridAdatper() {
            @Override
            public View getView(int index) {
                View view = LayoutInflater.from(SplashActivity.this).inflate(R.layout.login_item, null);
                ImageView image_item = (ImageView) view.findViewById(R.id.login_image_item);
                image_item.setImageResource(srcs[index]);
                return view;
            }

            @Override
            public int getCount() {
                return srcs.length;
            }
        });

        handler.postDelayed(r, 500);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(4000);
                    ToActivityUtils.startToActivity(SplashActivity.this, MainActivity.class);
                    finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    @Override
    protected void listener() {

    }
    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }
}
